package com.base.myproject.client.HR.attendance;

/**
 * 调整加班补休
 */
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BaseTreeStore;
import com.base.myproject.client.busi.Editdisable;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.busi.BO.TreeNodeModelData;
import com.base.myproject.client.jdbc.DataSet;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.BaseResource;
import com.base.myproject.client.system.Env;
import com.base.myproject.client.tools.CommandSyncContainer;
import com.base.myproject.client.tools.CommandSyncReturnObject;
import com.base.myproject.client.tools.CommandSyncsql;
import com.base.myproject.client.tools.GreetingService;
import com.base.myproject.client.tools.GreetingServiceAsync;
import com.base.myproject.client.tools.Tools;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.base.myproject.client.ui.celleditor.BaseComboBoxX;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.event.TreePanelEvent;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.DateTimePropertyEditor;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.StoreFilterField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckCascade;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckNodes;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Label;

public class Att_Work_And_Rest_Forrenshibu extends BaseJDBCPanel {

	// 创建一个TabPanel
	String tree_depcode = null;// 讲当前树形结构中选中的部门id保存
	String tree_de_name = null;// 讲当前树形结构中选中的部门name保存
	TabPanel tabPanel = null;
	TreePanel<TreeNodeModelData> treePanel = null;
	BaseTreeStore<TreeNodeModelData> treeWorkStore = null;
	BaseComboBoxX bc2;// 申请人下拉框
	BaseComboBoxX bsb12;// 班次
	BaseComboBoxX bsb13;// 申请类型
	BaseComboBoxForm<BaseModelData> rs = new BaseComboBoxForm<BaseModelData>("id", "name", "systeminfo","", true);
	DateField seach_begin_date = new DateField();// 查询开始日期
	DateField seach_end_date = new DateField();// 查询结束日期
	DateField adds_date = new DateField();// 批量添加发生日期
	TextField<String> seach_name = new TextField<String>();// 姓名输入框
	Button seach_bt = new Button("查询");// 查询按钮

	Button bt1 = new Button("批量同意");// 查询按钮
	Button bt2 = new Button("批量不同意");// 查询按钮
	Button btnAdds = new Button("批量添加");

	DateField date_t2 = new DateField();// 查询日期
	// 查询时用的申请类型
	BaseComboBoxX work_type = new BaseComboBoxX("name", "dictionary",
			"where type='hr_work_type'");
	NumberField date_score = new NumberField();// 时长
	// 创建树形结构的数据表
	StoreFilterField<BusiObject> ajaxfield;// 类似ajax的自动查询框
	PagingToolBar toolBar1 = null;

	private String bcode = Env.getEnvbcode();
	private String results = null;
	
	public Att_Work_And_Rest_Forrenshibu() {
		String sql = "select substring('id like '''+replace(permissionsbcode,';','%'' or id like '''),9," +
		"len('id like '''+replace(permissionsbcode,';','%'' or id like '''))-21) as result from" +
		" busipermissions where bcode = '"+bcode+"' and busicode = 8000";
		final com.base.myproject.client.tools.GreetingServiceAsync greetingService2 = GWT.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService2.getDataSet(sql, new AsyncCallback<DataSet>(){
		@Override
			public void onFailure(Throwable caught) {	
			}
			@Override
			public void onSuccess(DataSet result) {  
				if(result.getRowCount() != 0){
					results = result.getValue(0, "result");
					init();
				}else{
					MessageBox.alert("提示", "抱歉，您没有查看权限", null);
				}
			}
		});
	}

	public void init() {
		initTreePanel();
		initWeigetx();
		registerMainStore(store);
		registerStore(store);
	}

	public void complete() {
		super.complete();
	}

	private void initWeigetx() {
		toolBar1 = new PagingToolBar(10);

		// 垂直布局
		VerticalPanel vp = new VerticalPanel();
		HorizontalPanel s = new HorizontalPanel();

		s.setHeight("25");
		vp.setSpacing(5);
		s.setVerticalAlign(VerticalAlignment.MIDDLE);
		s.add(new Label("查询时段:"));
		seach_begin_date.setPropertyEditor(new DateTimePropertyEditor(
				"yyyy-MM-dd"));
		seach_begin_date.setWidth(100);
		s.add(seach_begin_date);
		s.add(new Label("至"));
		seach_end_date.setPropertyEditor(new DateTimePropertyEditor(
				"yyyy-MM-dd"));
		seach_end_date.setWidth(100);
		s.add(seach_end_date);
		work_type.setWidth(65);
		s.add(new Label("类型:"));
		s.add(work_type);
		s.add(new Label("匹配姓名:"));
		seach_name.setWidth(60);
		s.add(seach_name);
		seach_bt.addListener(Events.OnClick, getListener());
		s.add(seach_bt);
		bt1.addListener(Events.OnClick, getListener());
		s.add(bt1);
		bt2.addListener(Events.OnClick, getListener());
		s.add(bt2);
		s.add(new Label("发生日期："));
		adds_date.setWidth(100);
		adds_date.setToolTip("默认发生日期");
		adds_date.setPropertyEditor(new DateTimePropertyEditor("yyyy-MM-dd"));
		s.add(adds_date);
		s.add(btnAdds);
		btnAdds.addListener(Events.OnClick, getListener());

		vp.add(s);
		ContentPanel panel2 = new ContentPanel();
		panel2.setHeaderVisible(false);
		panel2.add(this.getBaseGrid());
		panel2.setBottomComponent(toolBar1);

		vp.add(panel2);
		HorizontalPanel cp2 = new HorizontalPanel();
		cp2.setScrollMode(Scroll.AUTOX);
		cp2.setSpacing(2);
		cp2.add(this.getBaseFormPanel());
		cp2.add(getBaseGrid_att());
		vp.add(cp2);

		BorderLayout layout = new BorderLayout();
		this.setLayout(layout);
		BorderLayoutData northData = new BorderLayoutData(LayoutRegion.WEST,
				200);
		northData.setCollapsible(true);
		northData.setFloatable(true);
		northData.setHideCollapseTool(true);
		northData.setSplit(true);
		northData.setMargins(new Margins(5, 5, 0, 0));

		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(5, 5, 0, 0));

		ContentPanel center = new ContentPanel();
		center.setHeaderVisible(false);
		center.add(vp);

		ContentPanel panel = new ContentPanel();
		panel.setHeading("选择部门");
		panel.setCollapsible(true);
		panel.add(getTreePanel());

		add(panel, northData);
		add(center, centerData);
		
		this.layout();
	}

	// 创建树形结构的数据表
	BaseTreeStore<TreeNodeModelData> treeStore = null;

	TreePanel<TreeNodeModelData> getTreePanel() {
		if (treePanel == null) {
			initTreePanel();
		}
		return treePanel;
	}

	BaseTreeStore<TreeNodeModelData> getTreeStore() {
		if (treeStore == null) {
			initTreeStore();
		}
		return treeStore;
	}

	// 创建班次数据连接
	BaseTreeStore<TreeNodeModelData> initTreeStore() {
		StoreInfo si = new StoreInfo();
		si.setSql("select icode,id,name from SystemInfo where id like "+results+" order by id asc");
		si.setTreesortcolumn("id");
		si.setKeyname(new String[] { "icode" });
		si.setTablename("SystemInfo");
		treeStore = new BaseTreeStore<TreeNodeModelData>(si, false);
		treeStore.load();
		return treeStore;

	}

	void initTreePanel() {
		treePanel = new TreePanel<TreeNodeModelData>(getTreeStore());
		treePanel.setWidth(200);
		treePanel.setHeight(445);
		treePanel.setBorders(false);
		treePanel.setDisplayProperty("name");
		treePanel.setCheckStyle(CheckCascade.PARENTS);
		treePanel.setCheckNodes(CheckNodes.BOTH);
		treePanel.getStyle().setLeafIcon(
				AbstractImagePrototype
						.create(BaseResource.INSTANCE.tree_file()));
		treePanel.setCheckable(false);
		treePanel.setAutoLoad(true);
		treePanel.setCaching(true);

		treePanel.addListener(Events.OnClick,
				new Listener<TreePanelEvent<TreeNodeModelData>>() {
					public void handleEvent(TreePanelEvent<TreeNodeModelData> be) {
						String id = be.getItem().get("id").toString();
						tree_depcode = id;
						tree_de_name = be.getItem().get("name").toString();
						String sql = "select * from hr_Attendance_ot where depCode like '" + id + "' and 1=2";
						grid.getStore().getStoreinfo().setSql(sql);
						grid.getStore().reload();
						bc2.setFilter(" WHERE b.bcode = a.bcode and b.employstatus = '在职' and a.systemid like '"
										+ tree_depcode + "%25'");
						bsb12.setFilter(" WHERE depCode like '" + tree_depcode
								+ "%25'");
					}
				});
	}

	public void setCheckNodes(CheckNodes checkNodes) {
		treePanel.setCheckNodes(checkNodes);
	}

	public void set_grid(String id) {
		// 配置加班补休表更新
		String bdate = seach_begin_date.getRawValue();
		String edate = seach_end_date.getRawValue();
		String name = seach_name.getRawValue();
		String str_type = work_type.getRawValue();

		String where = "";
		if (id.length() < 5) {
			where = where + "where depCode like '" + id + "%25'";
		} else {
			where = where + "where depCode = '" + id + "'";
		}
		if (!bdate.equals("")) {
			where += " and useDate >= '" + bdate + "' ";
		}
		if (!edate.equals("")) {
			where += " and useDate <= '" + edate + "' ";
		}
		if (!name.equals("")) {
			where += " and name like '%25" + name + "%25'";
		}
		if (!str_type.equals("")) {
			where += " and type = '" + str_type + "'";
		} else {
			where += " and type != '请假'";
		}
		String sql = "select * from hr_Attendance_ot " + where + " order by useDate desc";
		grid.getStore().getStoreinfo().setSql(sql);
		grid.getStore().reload();
	}

	// 创建一个数据表对象
	BaseStore<BusiObject> store = null;

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		store.setToolBar(toolBar1);
		return store;
	}

	ModelType type = new ModelType();

	BaseStore<BusiObject> createStore() {

		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");

		type.addField("icode");
		type.getField("icode").setType(Long.class);

		type.addField("signDate");
		type.getField("signDate").setType(Date.class);

		type.addField("bcode");
		type.getField("bcode").setType(String.class);

		type.addField("name");
		type.getField("name").setType(String.class);

		type.addField("depCode");
		type.getField("depCode").setType(String.class);

		type.addField("department");
		type.getField("department").setType(String.class);

		type.addField("useDate");
		type.getField("useDate").setType(Date.class);

		type.addField("type");
		type.getField("type").setType(String.class);

		type.addField("Bgntime1");
		type.getField("Bgntime1").setType(Date.class);

		type.addField("Endtime1");
		type.getField("Endtime1").setType(Date.class);

		type.addField("Bgntime2");
		type.getField("Bgntime2").setType(Date.class);

		type.addField("Endtime2");
		type.getField("Endtime2").setType(Date.class);

		type.addField("status");
		type.getField("status").setType(String.class);

		type.addField("batchNo");
		type.getField("batchNo").setType(String.class);

		type.addField("period");
		type.getField("period").setType(Double.class);

		type.addField("C_period");
		type.getField("C_period").setType(Double.class);

		type.addField("remark");
		type.getField("remark").setType(String.class);

		type.addField("readOnly");
		type.getField("readOnly").setType(String.class);

		StoreInfo info = new StoreInfo();
		info.setTablename("hr_Attendance_ot");
		info.setKeyname(new String[] { "icode" });
		info
				.setSql("select * from hr_Attendance_ot where 1=2 order by useDate asc");
		store = new BaseStore<BusiObject>(type, info);
		store.addEditdisableItem(new Editdisable("readOnly", "1",
				BaseStore.EditDisableType.IN));
		return store;
	}

	// 创建一个grid
	BaseGrid<BusiObject> grid = null;

	BaseGrid<BusiObject> getBaseGrid() {
		if (grid == null) {
			createGrid();
		}

		return grid;
	}

	DateField dateField1;
	CheckBoxSelectionModel<BusiObject> cbs;

	void createGrid() {
		cbs = new CheckBoxSelectionModel<BusiObject>();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		dateField1 = new DateField();

		ColumnConfig C11 = new ColumnConfig("status", "审批状态", 60);
		columns.add(C11);
		
		ColumnConfig C30 = new ColumnConfig("department", "部门", 100);
		columns.add(C30);

		ColumnConfig C2 = new ColumnConfig("bcode", "员工编号", 60);
		columns.add(C2);

		ColumnConfig C3 = new ColumnConfig("name", "申请人", 60);
		GridCellRenderer<ModelData> renderer = new GridCellRenderer<ModelData>() {
			public String render(ModelData model, String property,
					ColumnData d, int rowIndex, int colIndex,
					ListStore<ModelData> store, Grid<ModelData> grid) {
				if (model.get(property) != null
						&& model.get(property) instanceof ModelData) {
					return ((ModelData) model.get(property)).get("value");
				}
				return model.get(property);
			}
		};
		C3.setRenderer(renderer);
		columns.add(C3);

		ColumnConfig C5 = new ColumnConfig("useDate", "发生日期", 80);
		C5.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		columns.add(C5);

		ColumnConfig C6 = new ColumnConfig("type", "申请类型", 80);
		C6.setRenderer(renderer);
		columns.add(C6);

		ColumnConfig C13 = new ColumnConfig("period", "时长", 40);
		C13.setEditor(new CellEditor(date_score));
		columns.add(C13);

		ColumnConfig C14 = new ColumnConfig("C_period", "计算时长", 80);
		columns.add(C14);

		ColumnConfig C7 = new ColumnConfig("Bgntime1", "起始1", 50);
		C7.setDateTimeFormat(DateTimeFormat.getFormat("HH:mm"));
		columns.add(C7);

		ColumnConfig C8 = new ColumnConfig("Endtime1", "结束1", 50);
		C8.setDateTimeFormat(DateTimeFormat.getFormat("HH:mm"));
		columns.add(C8);

		ColumnConfig C9 = new ColumnConfig("Bgntime2", "起始2", 50);
		C9.setDateTimeFormat(DateTimeFormat.getFormat("HH:mm"));
		columns.add(C9);

		ColumnConfig C10 = new ColumnConfig("Endtime2", "结束2", 50);
		C10.setDateTimeFormat(DateTimeFormat.getFormat("HH:mm"));
		columns.add(C10);

		ColumnConfig C12 = new ColumnConfig("batchNo", "班次", 40);
		C12.setRenderer(renderer);
		columns.add(C12);

		ColumnConfig C1 = new ColumnConfig("signDate", "填表日期", 80);
		C1.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		C1.setHidden(true);
		C1.setEditor(new CellEditor(dateField1));
		columns.add(C1);

		ColumnConfig C15 = new ColumnConfig("remark", "说明", 80);
		C15.setHidden(true);
		columns.add(C15);

		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid<BusiObject>(getStore(), cm) {
			@SuppressWarnings("unchecked")
			public void addnewrecord() {
				if (tree_depcode == null) {
					MessageBox.alert("警告", "请选择一个部门后再添加新信息", null);
					return;
				}
				
				if (tree_depcode.length() != 6) {
					MessageBox.alert("警告", "添加只能对小部门进行操作，请选择小部门后再操作，谢谢！",
							null);
					return;
				}
				
				HashMap hp = new HashMap();
				Date df_date = null;
				if (adds_date.getValue() == null) {
					df_date = new Date();
				} else {
					df_date = adds_date.getValue();
				}

				hp.put("signDate", df_date);
				hp.put("depCode", tree_depcode);
				hp.put("department", tree_de_name);
				grid.getStore().add(hp);
			}
		};
		
		MenuItem delete = new MenuItem();
		delete.setText("批量删除加班/补休");
		delete.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE.deleterecord()));
		delete.addSelectionListener(new SelectionListener<MenuEvent>() {
			public void componentSelected(MenuEvent ce) {
				int idx = 0;
				for (BusiObject model : grid.getSelectionModel().getSelectedItems()) {
					if (model.get("readOnly") != null && model.get("readOnly").equals("1")) continue;
					idx = grid.getStore().indexOf(
							model);
					grid.getStore().remove(grid.getStore().getAt(idx));
				}
				save();
				grid.getStore().reload();
			}
		});
		
		grid.getMenu().add(delete);
		cbs.setSelectionMode(SelectionMode.MULTI);
		grid.addListener(Events.AfterEdit, getListener());
		grid.addListener(Events.OnClick, getListener());
		grid.setSelectionModel(cbs);
		grid.addPlugin(cbs);
		grid.setLoadMask(true);
		grid.setBorders(false);
		Tools.setResize(grid, 0.85, 0.5);
	}

	// 创建一个数据表对象--考勤视图表
	BaseStore<BusiObject> store_att = null;

	BaseStore<BusiObject> getStore_att() {
		if (store_att == null) {
			createStore_att();
		}
		return store_att;
	}

	BaseStore<BusiObject> createStore_att() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");

		type.addField("SSN");
		type.getField("SSN").setType(String.class);
		type.addField("checktype");
		type.getField("checktype").setType(String.class);
		type.addField("checktime");
		type.getField("checktime").setType(Date.class);
		type.addField("touse");
		type.getField("touse").setType(String.class);

		StoreInfo info = new StoreInfo();
		info.setTablename("attendance_view");
													// '2002/07/12 ')
		info.setSql("select right(rtrim(SSN),4) SSN,checktype,checktime,touse from attendance_view where 1=2");

		/* info.setSql("select right(rtrim(SSN),4) SSN,checktype,checktime,touse from attendance_view where right(rtrim(SSN),4)='{bcode}'"
		 + " and checktime  "+
		 " between '{useDate}'" +
		 " and dateadd(day,2,'{useDate}')");
		 
		info.setParent(store);// 关联主表
		info.setLinkparentcolumns(new String[] { "bcode","useDate" });
		// 关联主表字段
		info.setLinkparent_chile_columns(new String[] { "bcode","checktime"
		});// 关联字表字段
*/		store_att = new BaseStore<BusiObject>(type, info);
		return store_att;
	}

	BaseGrid<BusiObject> grid_att = null;

	BaseGrid<BusiObject> getBaseGrid_att() {
		if (grid_att == null) {
			creategrid_att();
		}

		return grid_att;
	}

	BaseGrid creategrid_att() {

		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

		ColumnConfig C1 = new ColumnConfig("checktime", "打卡时间", 80);
		C1.setDateTimeFormat(DateTimeFormat.getFormat("MM-dd HH:mm"));
		columns.add(C1);

		ColumnConfig C2 = new ColumnConfig("checktype", "上下班", 80);
		columns.add(C2);
		ColumnModel cm = new ColumnModel(columns);
		grid_att = new BaseGrid<BusiObject>(getStore_att(), cm, false, false);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		grid_att.setSelectionModel(cbs);
		// grid_att.setSize(200, 180);
		Tools.setResize(grid_att, 0.18, 0.35);
		return grid_att;
	}

	// 创建一个form
	BaseFormPanel form = null;
	DateField C_data8 = new DateField(); // 结束时间1
	TextField<String> C_2 = new TextField<String>();// 员工编号
	DateField C_data7 = new DateField(); // 开始时间1
	NumberField C_num14 = new NumberField(); // 计算时长
	DateField C_data9 = new DateField(); // 开始时间2
	DateField C_data10 = new DateField(); // 结束时间2
	NumberField C_num13 = new NumberField(); // 时长
	DateField C_data1 = new DateField(); // 填表日期
	DateField C_data5;
	TextField<String> C_num11;// 审批状态

	BaseFormPanel getBaseFormPanel() {
		if (form == null) {
			createForm();
		}
		return form;
	}

	BaseComboBoxX C_num6;// 申请类型

	void createForm() {
		form = new BaseFormPanel(getStore(), false);
		TableLayout tl = new TableLayout(6);
		TableData td = new TableData();
		td.setRowspan(1);
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.TOP);
		td.setPadding(2);
		form.setLayout(tl);
		form.setHeaderVisible(false);
		// form.setSize(605, 180);
		Tools.setResize(form, 0.60, 0.35);

		form.add(new Label("申请人:"), td);
		bc2 = new BaseComboBoxX("a.bname", "a.bcode", "bcode a, bcode_job b",
				" WHERE b.bcode = a.bcode and b.employstatus = '在职' and a.systemid like '"
						+ tree_depcode + "%25'");
		bc2.addListener(Events.Changed, getListener());
		bc2.setAllowBlank(true);
		bc2.setName("name");
		bc2.setWidth(127);
		form.add(bc2, td);

		form.add(new Label("员工编号:"), td);
		C_2.setReadOnly(true);
		C_2.setFireChangeEventOnSetValue(true);
		C_2.setName("bcode");
		C_2.setWidth(127);
		form.add(C_2, td);

		form.add(new Label("部门名称:"), td);
		TextField<String> C_num4 = new TextField<String>();
		C_num4.setReadOnly(true);
		C_num4.setName("department");
		C_num4.setWidth(127);
		form.add(C_num4, td);

		form.add(new Label("申请类型:"), td);
		C_num6 = new BaseComboBoxX("name", "dictionary","where type='hr_work_type'");
		C_num6.addListener(Events.Changed, getListener());
		C_num6.setAllowBlank(true);
		C_num6.setName("type");
		C_num6.setWidth(127);
		form.add(C_num6, td);

		form.add(new Label("发生日期:"), td);
		C_data5 = new DateField();
		C_data5.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-MM-dd"));
		C_data5.addListener(Events.Changed, getListener());
		C_data5.setAllowBlank(true);
		C_data5.setName("useDate");
		C_data5.setWidth(127);
		form.add(C_data5, td);

		form.add(new Label("填表日期:"), td);
		C_data1.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-MM-dd"));
		C_data1.setReadOnly(true);
		C_data1.setName("signDate");
		C_data1.setWidth(127);
		form.add(C_data1, td);

		form.add(new Label("班次:"), td);
		bsb12 = new BaseComboBoxX("batchNo", "hr_Attendance_WorkBatch", "");
		bsb12.setName("batchNo");
		bsb12.addListener(Events.Changed, getListener());
		bsb12.setWidth(127);
		form.add(bsb12, td);

		form.add(new Label("计算时长:"), td);
		C_num14.setPropertyEditorType(Double.class);
		C_num14.setFireChangeEventOnSetValue(true);
		C_num14.setName("C_period");
		C_num14.setReadOnly(true);
		C_num14.setWidth(127);
		form.add(C_num14, td);

		form.add(new Label("时长:"), td);
		C_num13.setPropertyEditorType(Double.class);
		C_num13.addListener(Events.Changed, getListener());
		C_num13.setFireChangeEventOnSetValue(true);
		C_num13.setName("period");
		C_num13.setWidth(127);
		form.add(C_num13, td);

		form.add(new Label("起始时间1:"), td);
		C_data7.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-MM-dd HH:mm"));
		C_data7.addListener(Events.Changed, getListener());
		C_data7.setFireChangeEventOnSetValue(true);
		C_data7.setName("Bgntime1");
		C_data7.setWidth(127);
		form.add(C_data7, td);

		form.add(new Label("结束时间1:"), td);
		C_data8.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-MM-dd HH:mm"));
		C_data8.addListener(Events.Changed, getListener());
		C_data8.setFireChangeEventOnSetValue(true);
		C_data8.setName("Endtime1");
		C_data8.setWidth(127);
		form.add(C_data8, td);

		form.add(new Label("审批状态:"), td);
		C_num11 = new TextField<String>();
		C_num11.setReadOnly(true);
		C_num11.setName("status");
		C_num11.setWidth(127);
		form.add(C_num11, td);

		form.add(new Label("起始时间2:"), td);
		C_data9.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-MM-dd HH:mm"));
		C_data9.addListener(Events.Changed, getListener());
		C_data9.setFireChangeEventOnSetValue(true);
		C_data9.setName("Bgntime2");
		C_data9.setWidth(127);
		form.add(C_data9, td);

		form.add(new Label("结束时间2:"), td);
		C_data10.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-MM-dd HH:mm"));
		C_data10.addListener(Events.Changed, getListener());
		C_data10.setFireChangeEventOnSetValue(true);
		C_data10.setName("Endtime2");
		C_data10.setWidth(127);
		form.add(C_data10, td);

		form.add(new Label("说明:"), td);
		TextField<String> C_num15 = new TextField<String>();
		C_num15.setName("remark");
		C_num15.setWidth(127);
		form.add(C_num15, td);
		form.setIsdisablebystore(true);
		form.inited();
	}

	/**
	 *根据选择的人员名字获得人员编码
	 */
	public void get_bcode(String bcode) {
		String sql2 = "select a.bcode from bcode a,bcode_job b where a.bcode = b.bcode and b.employstatus = '在职' and a.bcode='" + bcode + "' and a.systemid like '" + tree_depcode + "%'";
		final com.base.myproject.client.tools.GreetingServiceAsync greetingService2 = GWT
				.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService2.getDataSet(sql2, new AsyncCallback<DataSet>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(DataSet result) {
				if (result.getValue(0, "bcode").equals("")) {// 返回结果为0
					Info.display("错误信息提示！", "批量排班出错!");
				} else {
					C_2.setValue(result.getValue(0, "bcode"));
					judge(C_data7, C_data8, C_data9, C_data10);
				}
			}
		});
	}

	public void get_time_size(DateField d1, DateField d2) {
		String t1 = C_data7.getRawValue();// 起始时间1 2010-07-10 12:00
		String t2 = C_data8.getRawValue();// 结束时间1 2010-07-10 12:00:00
		String t3 = C_data9.getRawValue();// 起始时间1 2010-07-10 12:00:00
		String t4 = C_data10.getRawValue();// 结束时间1 2010-07-10 12:00:00
		/*if (!d2.getRawValue().equals("")) {
			if (d1.getRawValue().equals("")) {
				d2.setRawValue("");
				MessageBox.alert("警告", "时间段为两个必须填写！", null);
				return;
			}*/
		/*
		 * 由于没有找到 时间控件的onchange事件 目前用的是OnBlur 这个事件 每点击一次会触发两次 所以我在这里加了条件
		 * 限制其中的一次出现
		 */
		float size = 0;
		float size1 = 0;
		float size2 = 0;
		if (!t1.equals("") && !t2.equals("")) {
			Date date1 = new Date();
			date1 = C_date(t1);
			Date date2 = new Date();
			date2 = C_date(t2);
			float t_size = (float) ((date2.getTime() - date1.getTime()) / (1000 * 60.0 * 60.0));
			GWT.log("t_size:" + t_size);
			size1 = t_size;
		}
		if (!t3.equals("") && !t4.equals("")) {
			Date date1 = new Date();
			date1 = C_date(t3);
			Date date2 = new Date();
			date2 = C_date(t4);
			float t_size = (float) ((date2.getTime() - date1.getTime()) / (1000 * 60.0 * 60.0));
			size2 = t_size;
		}
		size = size1 + size2;
		C_num14.setValue(size);
		C_num13.setValue(C_num14.getValue());
			//} end if
	}
	
	String across_date;
	private String getAcrossDate() {
		Date thisDate = C_data5.getValue();  //得到发生日期的Date类型
		long time = thisDate.getTime() + 86400000;  //让该日期加一天
		thisDate = new Date(time);  //得到加了一天后的日期
		DateTimeFormat format = DateTimeFormat.getFormat("yyyy-MM-dd");
		String across_date = format.format(thisDate);
		return across_date;
	}

	/**
	 * 当选择固定班次的时候获得该班次的时间并附给form中的时间段,前提是班次和发生日期都不为空时才会进行设置， 这样做主要为了减少发送RPC
	 */
	public void get_batchNo(final boolean b) {
		if (!bsb12.getRawValue().equals("")
				&& !C_data5.getRawValue().equals("")) {
			setTimeReadOnly(edit);
			String sql = "select batchNo,bgnTime1,endTime1,bgnTime2,endTime2,span1,span2,calculation_length,working_hours from hr_Attendance_WorkBatch where batchNo='"
					+ bsb12.getRawValue().toUpperCase() + "' and depCode like '" + tree_depcode + "%'";
			
			final com.base.myproject.client.tools.GreetingServiceAsync greetingService2 = GWT
					.create(com.base.myproject.client.tools.GreetingService.class);
			greetingService2.getDataSet(sql, new AsyncCallback<DataSet>() {
				@Override
				public void onFailure(Throwable caught) {
				}

				@Override
				public void onSuccess(DataSet result) {
					String bgnTime1 = result.getValue(0, "bgnTime1");
					String endTime1 = result.getValue(0, "endTime1");
					String bgnTime2 = result.getValue(0, "bgnTime2");
					String endTime2 = result.getValue(0, "endTime2");
					String calculation_length = result.getValue(0, "calculation_length");
					String working_hours = result.getValue(0, "working_hours");
					String span1 = result.getValue(0, "span1");
					String span2 = result.getValue(0, "span2");
					String this_date = C_data5.getRawValue();
					across_date = getAcrossDate();

					if (calculation_length != null) {
						C_num13.setValue(Double.parseDouble(working_hours));
						C_num13.setReadOnly(true);
					} else {
						C_num13.setValue(0.0);
						C_num13.setReadOnly(false);
					}
					
					if (working_hours != null) {
						C_num14.setValue(Double.parseDouble(calculation_length));
						C_num14.setReadOnly(true);
					} else {
						C_num14.setValue(0.0);
					}

					if (bgnTime1 != null) {
						String t1 = "";
						String t2 = "";
						t1 = this_date + " " + bgnTime1;
						if (span1.equals("否")) {
							t2 = this_date + " " + endTime1;
						} else {
							t2 = across_date + " " + endTime1;
						}
						
						
						Date d1 = new Date();
						d1 = C_date(t1);

						Date d2 = new Date();
						d2 = C_date(t2);
						C_data7.setValue(d1); // 可能复制有问题
						C_data8.setValue(d2);
					}
					if (bgnTime2 != null) {
						String t3 = "";
						String t4 = "";
						t3 = this_date + " " + bgnTime2;
						if (span2.equals("否")) {
							t4 = this_date + " " + endTime2;
						} else {
							t4 = across_date + " " + endTime2;
						}
						Date d3 = new Date();
						d3 = C_date(t3);
						Date d4 = new Date();
						d4 = C_date(t4);
						C_data9.setValue(d3);
						C_data10.setValue(d4);
					}
					
					if (b) {
						judge(C_data7, C_data8, C_data9, C_data10);
					}
				}
			});
		}
	}

	/**
	 * 过滤
	 * 
	 * @param store
	 * @param parent
	 * @param record
	 * @param property
	 * @param filter
	 * @return ajax 查询
	 */
	protected boolean Select(Store<BusiObject> store, BusiObject parent,
			BusiObject record, String property, String filter) {
		for (int i = 0; i < type.getFieldCount(); i++) {

			Object name = record.get(type.getField(i).getName());

			// TODO
			if (name != null
					&& name.toString().indexOf(filter.toLowerCase()) != -1) {
				return true;
			}
		}

		return false;
	}

	public void cread_field() {
		ajaxfield = new StoreFilterField<BusiObject>() {
			@Override
			protected boolean doSelect(Store<BusiObject> store,
					BusiObject parent, BusiObject record, String property,
					String filter) {

				return Select(store, parent, record, property, filter);
			}

			@Override
			protected void onFilter() {
				super.onFilter();

				// grid.getSelectionModel().select(0);
			}
		};
		ajaxfield.setAutoWidth(true);
		ajaxfield.setToolTip("对当前部门数据进行查找");
		ajaxfield.bind(getStore());
		this.ajaxfield = ajaxfield;
	}

	public Date C_date(String date) {
		Date d1 = new Date();
		d1.setYear(Integer.parseInt(date.substring(0, 4)) - 1900);
		d1.setMonth(Integer.parseInt(date.substring(5, 7)) - 1);
		d1.setDate(Integer.parseInt(date.substring(8, 10)));
		d1.setHours(Integer.parseInt(date.substring(11, 13)));
		d1.setMinutes(Integer.parseInt(date.substring(14, 16)));
		return d1;
	}

	/************************************/

	private void setTimeReadOnly(boolean edit) {
		C_data7.setReadOnly(edit);
		C_data8.setReadOnly(edit);
		C_data9.setReadOnly(edit);
		C_data10.setReadOnly(edit);
	}

	boolean edit = false; // 判断班次是否有选，如果有选，就让用户不能edit开始和结束时间框
	int sync = 0;
	Listener<BaseEvent> defaultlistener;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}

		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				sync++;
				try {
					if (sync == 1) {
						if (be.getType() == Events.Change) {
							
						} else if (be.getType() == Events.OnClick) {
							if (be.getSource().equals(bt1)) {
								for (BusiObject md : grid.getSelectionModel()
										.getSelectedItems()) {
									int index = grid.getStore().indexOf(md);
									grid.setValue(index, "status", "同意");
									C_num11.setRawValue("同意");
								}
							}
							
							if (be.getSource().equals(seach_bt)) {
								if (tree_depcode == null) {
									MessageBox.alert("提示", "请先选择一个所属部门后再操作!",
											null);
									return;
								}
								set_grid(tree_depcode);
							}
							
							if (be.getSource().equals(bt2)) {
								for (BusiObject md : grid.getSelectionModel()
										.getSelectedItems()) {
									int index = grid.getStore().indexOf(md);
									grid.setValue(index, "status", "不同意");
									C_num11.setRawValue("不同意");
								}
							}
							if (be.getSource().equals(grid)) {
								BusiObject bo1 = grid.getSelectionModel().getSelectedItem();
								if (bo1 == null) return;
								if (bo1.get("batchNo") != null) {
									C_num13.setReadOnly(true);
									C_num14.setReadOnly(true);
									setTimeReadOnly(true);
								} else {
									C_num13.setReadOnly(false);
									C_num14.setReadOnly(false);
									setTimeReadOnly(false);
								}
								
								GridEvent<BusiObject> ge = (GridEvent) be;
								int index = ge.getRowIndex();
								if (index == -1)
									return;
								String bcode = "";
								String m = "";
								String d = "";
								if (grid.getStore().getValue(index, "bcode") == null
										|| grid.getStore().getValue(index,
												"bcode") == "null") {
									return;
								} else {
									bcode = grid.getStore().getValue(index,
											"bcode").toString();
								}
								Date useDate = (Date) grid.getStore().getValue(
										index, "useDate");

								// 由于通过date转换使得月和日都是一位数了
								// 造成在比较的时候得不到内容所以用字符串拼接了一下
								if ((useDate.getMonth() + 1) < 10) {
									m = "0" + (useDate.getMonth() + 1);
								} else {
									m = String
											.valueOf((useDate.getMonth() + 1));
								}
								if ((useDate.getDate()) < 10) {
									d = "0" + (useDate.getDate());
								} else {
									d = String.valueOf((useDate.getDate()));
								}
								String date = (useDate.getYear() + 1900) + "-"
										+ m + "-" + d;
								String sql = "select right(rtrim(SSN),4) SSN,checktype,checktime,touse from attendance_view "
										+ "where right(rtrim(SSN),4)='"
										+ bcode
										+ "'"
										+ " and substring(convert(varchar(20),checktime,120),0,11)  "
										+ " between '"
										+ date
										+ "'"
										+ " and dateadd(day,2,'" + date + "') and touse = '考勤'";
								grid_att.getStore().getStoreinfo().setSql(sql);
								grid_att.getStore().reload();
							} else if (be.getSource().equals(btnAdds)) {
								Listener<MessageBoxEvent> addDateListener = new Listener<MessageBoxEvent>() {
									@Override
									public void handleEvent(MessageBoxEvent be) {
										adds_date.focus();
									}

								};
								if (tree_depcode == null) {
									MessageBox.alert("警告", "请先选择一个所属部门后再操作!",
											null);
									return;
								}
								
								if (tree_depcode.length() != 6) {
									MessageBox.alert("警告", "添加只能对小部门进行操作，请选择小部门后再操作，谢谢！",
											null);
									return;
								}

								if (adds_date.getRawValue().equals("")) {
									MessageBox.alert("警告", "请先一个默认发生日期再操作，谢谢",
											addDateListener);
									return;
								}
								String sql = "select bcode.bcode,bcode.bname from bcode,bcode_job where systemid='"
										+ tree_depcode
										+ "' and bcode.bcode"
										+ " = bcode_job.bcode and employstatus='在职'";
								final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
										.create(com.base.myproject.client.tools.GreetingService.class);
								greetingService.getDataSet(sql,
										new AsyncCallback<DataSet>() {

											@Override
											public void onFailure(
													Throwable caught) {

											}

											@Override
											public void onSuccess(DataSet result) {
												if (result.getRowCount() > 0) {
													BusiObject bo = null;
													String type = "加班";  //默认批量添加的类型；
													String onClickType = work_type.getRawValue();
													if (!onClickType.equals("")) {
														type = onClickType;
													}
													for (int i = 0; i < result
															.getRowCount(); i++) {
														bo = new BusiObject();
														bo
																.set(
																		"bcode",
																		result
																				.getValue(
																						i,
																						"bcode"));
														bo
																.set(
																		"name",
																		result
																				.getValue(
																						i,
																						"bname"));
														bo.set("depCode",
																tree_depcode);
														bo.set("department",
																tree_de_name);
														bo.set("signDate",
																new Date());
														bo.set("type", type);
														bo
																.set(
																		"useDate",
																		adds_date
																				.getValue());
														bo
																.set(
																		"Bgntime1",
																		adds_date
																				.getValue());
														bo
																.set(
																		"Endtime1",
																		adds_date
																				.getValue());
														grid.getStore().add(bo);
													}
													grid.getStore().reload();
												}
											}

										});

							}

						} else if (be.getType() == Events.Changed) {
							/*********************/
							String startTime1 = C_data7.getRawValue();
							String endTime1 = C_data8.getRawValue();
							String startTime2 = C_data9.getRawValue();
							String endTime2 = C_data10.getRawValue();
							
							if (be.getSource().equals(C_data7) || be.getSource().equals(C_data8)) {
								if (!startTime1.equals("") && !endTime1.equals("")) {
									if (be.getSource().equals(C_data8) && C_data7.getValue().getTime() >= C_data8.getValue().getTime()) {
										MessageBox.alert("提示",
												"结束时间1不能小于等于开始时间1，请重新填写结束时间1，谢谢！",
												null);
										C_data8.setRawValue("");
										return;
									}
									
									if (C_data7.getValue().getTime() < C_data8.getValue().getTime()) {
										judge(C_data7, C_data8);
									}
								}
								
								if (!startTime1.equals("") && C_data5.getValue() != null) {
									if (C_data7.getValue().before(C_data5.getValue())) {
										MessageBox.alert("提示",
												"开始时间1不能小于发生日期，请重新填写开始时间1，谢谢！",
												null);
										C_data7.setValue(C_data5.getValue());
										return;
									}
								}
								
								if (bsb12.getRawValue().equals("")) {// 如果已经选了班次则不需要计算时长
									get_time_size(C_data7, C_data8);
								}
								
							} else if (be.getSource().equals(C_data9) || be.getSource().equals(C_data10)) {
								if (C_data8.getValue().getTime() >= C_data9.getValue().getTime()) {
									MessageBox.alert("提示",
											"开始时间2不能小于等于结束时间1，请重新填写开始时间2，谢谢！",
											null);
									C_data9.setRawValue("");
									return;
								}
								
								if (!startTime2.equals("")) {
									if (C_data9.getValue().before(C_data5.getValue())) {
										MessageBox.alert("提示",
												"开始时间2不能小于发生日期，请重新填写开始时间2，谢谢！",
												null);
										C_data9.setValue(C_data5.getValue());
										return;
									}
								}
								
								if (!startTime2.equals("") && !endTime2.equals("")) {
									if (be.getSource().equals(C_data10) && C_data9.getValue().getTime() >= C_data10.getValue().getTime()) {
										MessageBox.alert("提示",
												"结束时间2不能小于等于开始时间2，请重新填写结束时间2，谢谢！",
												null);
										C_data10.setRawValue("");
										return;
									}
									
									if (C_data9.getValue().getTime() < C_data10.getValue().getTime()) {
										judge(C_data9, C_data10);
									}
								}
								
								if (bsb12.getRawValue().equals("")) {// 如果已经选了班次则不需要计算时长
									get_time_size(C_data9, C_data10);
								}
							} else if (be.getSource().equals(C_data5)) {
								// 当C_data5不为空时，进行以下判断
								if (!C_data5.getRawValue().equals("")) {
									if (!bsb12.getRawValue().equals("")) {
										get_batchNo(false);
										edit = true;
									} else {
										C_data7
												.setValue(C_data5
														.getValue());
										C_data8
												.setValue(C_data5
														.getValue());
										edit = false;
									}
									// C_data5为空时，就设置C_data7和C_data8为空
								} else {
									C_data7.setRawValue("");
									C_data8.setRawValue("");
									C_num13.setValue(0.0);
									C_num14.setValue(0.0);
									edit = false;
								}
								setTimeReadOnly(edit);
								judge(C_data7, C_data8, C_data9, C_data10);
							} else if (be.getSource().equals(bsb12)) {
								if (!bsb12.getRawValue().equals("")) {
									String sql = "select count(icode) as icode  from hr_Attendance_WorkBatch where batchNo='"
											+ bsb12.getRawValue().toUpperCase()
											+ "'";
									final com.base.myproject.client.tools.GreetingServiceAsync greetingService2 = GWT
											.create(com.base.myproject.client.tools.GreetingService.class);
									greetingService2.getDataSet(sql,
											new AsyncCallback<DataSet>() {
												@Override
												public void onFailure(
														Throwable caught) {
												}

												@Override
												public void onSuccess(
														DataSet result) {
													if (result.getValue(0,
															"icode")
															.equals("0")) {// 返回结果为0
														bsb12.setRawValue("");
														edit = false;
														MessageBox.alert("警告",
																"班次代码有误,请检查！",
																null);
														return;
													} else {
														edit = true;
														get_batchNo(true);
													}
												}
											});
								} else {
									C_num13.setValue(0.0);
									C_num14.setValue(0.0);
									C_num13.setReadOnly(false);
									C_num14.setReadOnly(false);
									edit = false;

									C_data7.setValue(C_data5.getValue());
									C_data8.setValue(C_data5.getValue());
									C_data9.setRawValue("");
									C_data10.setRawValue("");
									setTimeReadOnly(edit);
								}
							} else if (be.getSource().equals(bc2)) {
								get_bcode(bc2.getValue().get("extdisplayname").toString());
							} else if (be.getSource().equals(C_num13)) {
								if (!C_num13.getRawValue().equals("")) {
									float a = Float.parseFloat(C_num13
											.getRawValue());
									float b = Float.parseFloat(C_num14
											.getRawValue());
									if (type.equals("调整")) {
										C_num14.setValue(a);
									} else {
										if (a > b) {
											C_num13.setValue(b);
											MessageBox.alert("警告",
													"'时长'不能大于'计算时长'！", null);
											return;
										}
									}
								}
							} else if (be.getSource().equals(C_num6)) {
								judge(C_data7, C_data8, C_data9, C_data10);
							}
							/*************************/

						} else if (be.getType() == Events.Select) {
							
						} else if (be.getType() == Events.AfterEdit) {
							GridEvent<BusiObject> ge = (GridEvent) be;
							int index = ge.getRowIndex();
							if (ge.getProperty().equals("period")) {// 可同时监听多个字段ge.getProperty().equals("amount")
																	// ||
								float a = Float.parseFloat(grid.getValue(index,
										"period").toString());
								float b = Float.parseFloat(C_num14
										.getRawValue());
								if (a > b) {
									grid.setValue(index, "period", b);
									MessageBox.alert("警告", "'时长'不能大于'计算时长'！",
											null);
									return;
								}
							}
						} else if (be.getType() == Events.KeyUp) {
							if (be instanceof ComponentEvent) {
								if (((ComponentEvent) be).getKeyCode() == 13) // 回车
								{
								}
							}
						}
					}
				} finally {
					sync--;
				}
			}
		};
		return defaultlistener;
	}
	
	
	/**
	 * @param startDate1 	开始时间1
	 * @param endDate1	结束时间1
	 * @param startDate2	开始时间2
	 * @param endDate2	结束时间2
	 * 当发生Changed事件的组件不为时间控件的话，调用以下方法
	 */
	private void judge(DateField startDate1, DateField endDate1, DateField startDate2, DateField endDate2) {
		if (!startDate1.getRawValue().equals("") && !endDate1.getRawValue().equals("")) {
			judge(startDate1, endDate1);
		}
		
		if (!startDate2.getRawValue().equals("") && !endDate2.getRawValue().equals("")) {
			judge(startDate2, endDate2);
		}
	}
	
	/**
	 * @param date1 时间开始控件
	 * @param date2 时间结束控件
	 * 当发生Changed事件的组件为时间控件的话，调用以下方法
	 */
	private void judge(final DateField date1, final DateField date2) {
		final String code = C_2.getRawValue();
		final String type = C_num6.getRawValue();
		final String startTime1 = date1.getRawValue();
		final String endTime1 = date2.getRawValue();

		//这个SQL用来查询在这个type的时间段内有没相同类型的确type交叉数据，如果有就提示错误
		String sql = "select *  from hr_Attendance_ot where bcode='"
			+ code + "' and type = '" + type + "' and useDate between '" + startTime1.substring(0, 10) + "' and '" + endTime1.substring(0, 10) + "' and ((bgntime1 < '" + endTime1 + "' and endtime1 > '" 
			+ startTime1 + "') or (bgntime2 < '" + endTime1 + "' and endtime2 > '" 
			+ startTime1 + "'))";
		
		final com.base.myproject.client.tools.GreetingServiceAsync greetingService2 = GWT
		.create(com.base.myproject.client.tools.GreetingService.class);
greetingService2.getDataSet(sql,
		new AsyncCallback<DataSet>() {
			@Override
			public void onFailure(
					Throwable caught) {
			}

			@Override
			public void onSuccess(
					DataSet result) {
				if (result.getRowCount() > 0) {// 返回结果为0
					MessageBox.alert("提示",
							"该时间段已经有" + type + "数据了，请重新设置时间段或选择其它班次，谢谢！",
							null);
					clearTime(date1, date2);
				} else {
					String workSql = "select *,'" + startTime1.substring(0, 7) + "-" + startTime1.substring(8,10) + "' as d1 from hr_Attendance_WorkBatch where batchNo = (select r" + (startTime1.substring(8,10).indexOf("0") == 0? startTime1.substring(9,10) : startTime1.substring(8,10)) 
					+ " from hr_Attendance_schedule where convert(varchar(7),ym,120) = '" + startTime1.substring(0, 7) + "' and bcode = '" + code + "') and depCode = '" + tree_depcode + "'";
					if (!startTime1.substring(0, 10).equals(endTime1.substring(0, 10))) {
						workSql = workSql + " union all "
						+ "select *,'" + endTime1.substring(0, 7) + "-" + endTime1.substring(8,10) + "' as d1 from hr_Attendance_WorkBatch where batchNo in (select r" + endTime1.substring(8,10) 
						+ " from hr_Attendance_schedule where convert(varchar(7),ym,120) = '" + endTime1.substring(0, 7) + "' and bcode = '" + code + "') and depCode = '" + tree_depcode + "'";
					}
					
					final com.base.myproject.client.tools.GreetingServiceAsync greetingService2 = GWT
					.create(com.base.myproject.client.tools.GreetingService.class);
			greetingService2.getDataSet(workSql,
					new AsyncCallback<DataSet>() {
						@Override
						public void onFailure(
								Throwable caught) {
						}

						@Override
						public void onSuccess(
								DataSet result) {
							if (result.getRowCount() > 0)
								for (int i=0; i<result.getRowCount(); i++) {
									String span1 = result.getValue(i, 9);
									String span2 = result.getValue(i, 10);
									String bgnTime2 = result.getValue(i, "bgnTime2");
									String endTime2 = result.getValue(i, "endTime2");
									String bgnTime1 = result.getValue(i, "bgnTime1");
									String endTime1 = result.getValue(i, "endTime1");
									String this_date = result.getValue(i, "d1");

									if (bgnTime1 != null) {
										String t1 = "";
										String t2 = "";
										t1 = this_date + " " + bgnTime1;
										if (span1.equals("否")) {
											t2 = this_date + " " + endTime1;
										} else {
											t2 = across_date + " " + endTime1;
										}
										
										Date d1 = new Date();
										d1 = C_date(t1);

										Date d2 = d1;
										d2 = C_date(t2);
										
										if (type.equals("加班")) {
											if (C_data8.getValue() != null && C_data7.getValue() != null && d1.before(C_date(C_data8.getRawValue())) && d2.after(C_date(C_data7.getRawValue()))) {
												MessageBox.alert("提示",
														"起始时间1到结束时间1的时间段和该员工的上班时间有交集，不能再加班，请重新选择班次或填写时间段",
														null);
												clearTime(C_data7, C_data8);
												return;
											}
											
											if (C_data9.getValue() != null && C_data10.getValue() != null && d1.before(C_date(C_data10.getRawValue())) && d2.after(C_date(C_data9.getRawValue()))) {
												MessageBox.alert("提示",
														"起始时间2到结束时间2的时间段包含了该员工的上班时间有交集，不能再加班。",
														null);
												clearTime(C_data9, C_data10);
												return;
											}
										} else if (type.equals("补休")) {
											//补休的话由于初始值时间为00：00时，开始时间小于排班的时间，所以会报错，现在加了个判断开始时间不等于结束时间才验证
											if (C_data8.getValue() != null && C_data7.getValue() != null && !C_data7.getRawValue().equals(C_data8.getRawValue()) && (d1.getTime() > C_date(C_data7.getRawValue()).getTime() || d2.getTime() < C_date(C_data8.getRawValue()).getTime())) {
												MessageBox.alert("提示",
														"起始时间1到结束时间1的时间段超出了该员工该天的上班时间，不能补休。",
														null);
												clearTime(C_data7, C_data8);
												return;
											}
											
											if (C_data9.getValue() != null && C_data10.getValue() != null && !C_data9.getRawValue().equals(C_data10.getRawValue()) && (d1.getTime() > C_date(C_data9.getRawValue()).getTime() || d2.getTime() < C_date(C_data10.getRawValue()).getTime())) {
												MessageBox.alert("提示",
														"起始时间2到结束时间2的时间段超出了该员工该天的上班时间，不能补休。",
														null);
												clearTime(C_data9, C_data10);
												return;
											}
										}
									}
									if (bgnTime2 != null) {
										String t3 = "";
										String t4 = "";
										t3 = this_date + " " + bgnTime2;
										if (span2.equals("否")) {
											t4 = this_date + " " + endTime2;
										} else {
											t4 = across_date + " " + endTime2;
										}
										Date d3 = new Date();
										d3 = C_date(t3);
										Date d4 = d3;
										d4 = C_date(t4);
										
										if (type.equals("加班")) {
											if (C_data7.getValue() != null && C_data8.getValue() != null && d3.before(C_date(C_data8.getRawValue())) && d4.after(C_date(C_data7.getRawValue()))) {
												MessageBox.alert("提示",
														"起始时间1到结束时间1的时间段包含了该员工的上班时间，不能再加班。",
														null);
												clearTime(C_data7, C_data8);
												return;
											}
											
											if (C_data9.getValue() != null && C_data10.getValue() != null && d3.before(C_date(C_data10.getRawValue())) && d4.after(C_date(C_data9.getRawValue()))) {
												MessageBox.alert("提示",
														"起始时间2到结束时间2的时间段包含了该员工的上班时间，不能再加班。",
														null);
												clearTime(C_data9, C_data10);
												return;
											}
										} else if (type.equals("补休")) {
											if (C_data7.getValue() != null && C_data8.getValue() != null && !C_data7.getRawValue().equals(C_data8.getRawValue()) && (d3.getTime() > C_date(C_data7.getRawValue()).getTime() || d4.getTime() < C_date(C_data8.getRawValue()).getTime())) {
												MessageBox.alert("提示",
														"起始时间1到结束时间1的时间段超出了该员工该天的上班时间，不能补休。",
														null);
												clearTime(C_data7, C_data8);
												return;
											}
											
											if (C_data9.getValue() != null && C_data10.getValue() != null && !C_data9.getRawValue().equals(C_data10.getRawValue()) && (d3.getTime() > C_date(C_data9.getRawValue()).getTime() || d4.getTime() < C_date(C_data10.getRawValue()).getTime())) {
												MessageBox.alert("提示",
														"起始时间2到结束时间2的时间段超出了该员工该天的上班时间，不能补休。",
														null);
												clearTime(C_data9, C_data10);
												return;
											}
										}
									}
								}
						}
					});
				}
			}
		});
	}
	
	/**
	 * @param startDate 要清除的开始时间控件
	 * @param endDate 要清除的结束时间控件
	 */
	private void clearTime(DateField startDate, DateField endDate) {
		startDate.setValue(C_data5.getValue());
		endDate.setValue(C_data5.getValue());
		startDate.setReadOnly(false);
		endDate.setReadOnly(false);
		
		store.setValue("batchNo", null);
		bsb12.setValue(null);
		C_num13.setValue(0.0);
		C_num14.setValue(0.0);
		C_num13.setReadOnly(false);
	}

	/************************/
	private boolean save_sql(String sql) {
		CommandSyncsql csql = new CommandSyncsql();// 执行sql命令
		String delsql = sql;
		csql.getV().add(delsql);
		if (csql.getV().size() <= 0) {
			return false;
		} else {
			CommandSyncContainer list = new CommandSyncContainer();// 可以添加的命令
			list.add(csql);

			GreetingServiceAsync greetingService = GWT
					.create(GreetingService.class);
			greetingService.SendCommandSync(list,
					new AsyncCallback<CommandSyncReturnObject>() {
						@Override
						public void onFailure(Throwable caught) {
							MessageBox.alert("错误", "网络连接失败", null);
						}

						@Override
						public void onSuccess(CommandSyncReturnObject result) {
							if (result.isB()) {
								Info.display("提示信息", "操作成功");
							} else {
								MessageBox.alert("错误", "错误，错误代码"
										+ result.getMessage(), null);
							}
						}
					});
		}
		return true;
	}
}
